class GAME_KUNKUN {
    #data = null;
    #setp = 0.1;
    constructor(p) {
        this.#data = p
    }
    get _data() {
        return this.#data;
    }
    set _plus(step) {
        this.#setp = step; // 设置步进
    }
    plusX() {
        this.#data.x += this.#setp;
        if (this.#data.x >= 1024) { return 1024 }
        return this.#data.x;
    }
    subX() {
        if (this.#data.x <= 0) { return 0 }
        this.#data.x -= this.#setp;
        return this.#data.x;
    }
}

function isArray(arr) {
    return Reflect.isArray(arr);
}

function isEmmtyObj(obj) {
    return !obj ? true : (Reflect.ownKeys(obj).length === 0 ? true : false);
}

function __def(el, obj, call) {
    if (!el) { return }
    if (!props) {
        return el.getBoundingClientRect();
    }
    if (obj && isEmmtyObj(props)) {
        for (const k in obj) {
            const val = obj[k];
        }
    }
}
function defProp(el, props) {
    return __def(el, props, "el.setAttribute(k, val);");
}
function defStyle(el, props) {
    if (!el) { return }
    if (!props) {
        return el.getBoundingClientRect();
    }
    if (obj && isEmmtyObj(props)) {
        for (const k in obj) {
            const val = obj[k];
            el.style[k] = val;
        }
    }
}

$(document).ready(function () {
    // 插件配置
    let width = 1024, height = 576;
    var player = null; // 玩家实例
    var keyboard = null;// 键盘输入事件
    const config = {
        type: Phaser.AUTO,
        width,
        height,
        physics: {
            default: 'arcade',
            arcade: {
                gravity: false,//{ y: 980 }
                debug: !0
            }
        },
        scene: {
            preload,
            create,
            update
        }
    }

    new Phaser.Game(config);
    function preload() {
        this.load.image("bg", "img/bg.jpg");
        this.load.image("kk_head", "img/kunkun_head.jpg");
        this.load.spritesheet("actor", "img/kunkun_body.png", {
            frameWidth: 58, frameHeight: 127
        });
        this.load.image("ball", "img/basketball.png");
    }
    let org = null;
    function create() {
        // 场景
        this.add.image(width / 2, height / 2, "bg");
        var image_plat = this.physics.add.staticGroup();
        image_plat.create(40, height - 40, "ball").setScale(0.5).refreshBody();
        // 角色
        player = this.physics.add.sprite(120, height, "actor").setScale(1.5);
        player.setBounce(0);
        player.setCollideWorldBounds(true);// 世界碰撞开关
        this.physics.add.collider(player, image_plat); //碰撞检测
        this.physics.add.overlap(player, image_plat, nearBall, null, this);
        // 键盘输入
        keyboard = this.input.keyboard.createCursorKeys();
        // 左走动画
        this.anims.create({
            key: "move_left",
            frames: this.anims.generateFrameNumbers("actor", { start: 0, end: 2 }),
            frameRate: 10,
            repeat: -1
        });
        // 转身动画
        this.anims.create({
            key: "turn",
            frames: [{
                key: "actor", frame: 3
            }],
            frameRate: 0,
        });

        // 右走动画
        this.anims.create({
            key: "move_right",
            frames: this.anims.generateFrameNumbers("actor", { start: 3, end: 5 }),
            frameRate: 10,
            repeat: -1
        });
        // const x = player.x,y = player.y,z=player.z;
        // org = new GAME_KUNKUN({
        //     x,
        //     y,
        //     z
        // });
        this.input.on("gameobjectmove",(e)=>{
            console.log("move",e)
        })
    }
    // 检测是否靠近篮球
    function nearBall(_player, _target) {
        console.log("拿到篮球了", _player, _target);
    }
    function update() {
        if (keyboard.left.isDown) {
            player.setVelocityX(-300); //
            player.anims.play("move_left");
        }
        else if (keyboard.right.isDown) {
            player.setVelocityX(300); //
            player.anims.play("move_right")
        }
    }
});